<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Model\AdminUser;
use App\Model\LogLogin;
use App\Model\LogSummary;
use App\Model\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Session;

class LoginController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function login(Request $request)
    {
        if ($request->method() == 'POST') {
            //检测邮箱
            $data = $request->post();
            $first = DB::table("admin_user")->where("user_name", $data['user_name'])->orWhere("email", $data['user_name'])->first();
            if (empty($first)) {
                return response()->json([
                    'code' => 0,
                    'msg' => '用户名或者邮箱还没注册'
                ]);
            } else {
                if (Hash::check($first->password, $data['password'])) {
                    return response()->json([
                        'code' => 0,
                        'msg' => '账号或则密码错误'
                    ]);
                }

                if ($first->status == '-1') {
                    return response()->json([
                        'code' => 0,
                        'msg' => '账号已被冻结，如有疑问请联系客服'
                    ]);
                }
                AdminUser::getInfo( $first->uid);
                return response()->json([
                    'code' => 1,
                    'msg' => '登陆成功'
                ]);

            }
        } else {
            return view("admin/login");
        }

    }

}
